//
//  Colors.swift
//  Game
//
//  Created by woniu on 2025/8/22.
//

import Foundation
import UIKit

struct Colors {
    
    static let title:UIColor = UIColor(hexString: "#2A2A2A")!
    static let title3:UIColor = UIColor(hexString: "#666666")!
    //主题蓝色
    static let blue:UIColor = UIColor(hexString: "#228EFF")!
    //浅灰色
    static let gray:UIColor = UIColor(hexString: "#f7f6f5")!
    //深色
    static let deepGray:UIColor = UIColor(hexString: "#606F7E")!
    //分割线(颜色很浅)
    static let line:UIColor = UIColor(hexString: "#F7F7F7")!
    //分割线2（颜色有点点深）
    static let line2:UIColor = UIColor(hexString: "#EAEAEA")!
    //背景
    static let bgColor:UIColor = UIColor(hexString: "#F2F2F2")!
    //背景2
    static let searchBGColor = UIColor(hexString: "#F5F7FA")!
    //绿色
    static let green:UIColor = UIColor(hexString: "#57BE6A")!
    //按钮蓝色
    static let buttonBlue = UIColor(red: 70/255.0, green: 137/255.0, blue: 255/255.0, alpha: 1)
    //青色
    static let cyan = UIColor(red: 0, green: 0.81, blue: 0.75, alpha: 1)
    //输入框占位颜色
    static let placeholderColor = UIColor(hexString: "#B1B3BE")!
    //不可用颜色，按钮不可用
    static let disabledColor =  UIColor(hexString: "#999999")!
    //红色
    static let red =  UIColor(hexString: "#FF0000")!
    //黄色
    static let yellow = UIColor(hexString: "#F3AD00")!
    
    ///随机色
    public static var randomColor:UIColor{
        let red = CGFloat(arc4random()%256)/255.0
        let green = CGFloat(arc4random()%256)/255.0
        let blue = CGFloat(arc4random()%256)/255.0
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
    
    
    // 生成随机暖色
    public static func getRandomWarmColor() -> UIColor {
        var colors: [UIColor] = [
            UIColor(red: 1.0, green: 0.6, blue: 0.4, alpha: 1.0), // 橙色
            UIColor(red: 1.0, green: 0.7, blue: 0.5, alpha: 1.0), // 浅橙色
            UIColor(red: 0.9, green: 0.6, blue: 0.4, alpha: 1.0), // 红橙色
            UIColor(red: 1.0, green: 0.5, blue: 0.3, alpha: 1.0), // 橙红色
            UIColor(red: 0.9, green: 0.7, blue: 0.5, alpha: 1.0)  // 奶油色
        ]
        colors.append(contentsOf: getColorsList())
        return colors[Int.random(in: 0..<colors.count)]
    }
    
    //
    private static func getColorsList()->[UIColor]{
        var colors:[String] = ["#2475C2","#D2A04F","#E67DFF","#74BC2D","#FF7D7B","#AE90FF","#65A5FF","#FF7E8C","#FFB450","#E68A55","#FF5E5B","#FFF1B8","#FFCDB2","#D4A373","#FF9F1C","#F8CECC","#FF9F1C"]
        colors.append(contentsOf: ["#FFA502","#FF7538","#FFF4D2","#D97706","#E76F51","#C89F91","#FFCDB3","#F4A261","#FFF4D2","#B76E79","#F0B90B","#C89F91","#FF7538","#FFF4D2",])
        colors.append(contentsOf: ["#E76F51","#F4A261","#FFA502","#D97706","#FFCDB3"])
        var colorList:[UIColor] = []
        for str in colors{
            colorList.append(UIColor(hexString: str)!)
        }
        
        return colorList
    }
}



extension UIColor {
    
    
    /// 把16进制转颜色
    /// - Parameters:
    ///   - hexString: 十六进制字符串
    ///   - alpha: 透明度
    public convenience init?(hexString: String, alpha: CGFloat = 1.0) {
        var formatted = hexString.replacingOccurrences(of: "0x", with: "")
        formatted = formatted.replacingOccurrences(of: "#", with: "")
        if let hex = Int(formatted, radix: 16) {
            let red = CGFloat(CGFloat((hex & 0xFF0000) >> 16)/255.0)
            let green = CGFloat(CGFloat((hex & 0x00FF00) >> 8)/255.0)
            let blue = CGFloat(CGFloat((hex & 0x0000FF) >> 0)/255.0)
            self.init(red: red, green: green, blue: blue, alpha: alpha)} else {
                return nil
        }
    }

    /// 颜色转十六进制字符串（不含透明度）
    public func toHexString() -> String {
        guard let components = cgColor.components else { return "#000000" }
        let r: CGFloat, g: CGFloat, b: CGFloat
        if components.count >= 3 {
            r = components[0]
            g = components[1]
            b = components[2]
        } else {
            // 灰度色
            r = components[0]
            g = components[0]
            b = components[0]
        }
        let ri = Int(round(r * 255))
        let gi = Int(round(g * 255))
        let bi = Int(round(b * 255))
        return String(format: "#%02X%02X%02X", ri, gi, bi)
    }
   
}
